package online.szlib.com.rest.controller;

import java.util.List;

import online.szlib.com.common.utils.JSONHelper;
import online.szlib.com.core.domain.Order;
import online.szlib.com.core.services.OrderService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
@RequestMapping("/api/orders")
public class OrderRestController {

    private static Logger logger = LoggerFactory.getLogger(OrderRestController.class);

    @Autowired
    private OrderService orderService;

	// Http Get All
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<Order> getAll() {
		System.out.println("List Orders >>>>>>>>>>>>>>>>>>>>");
		return orderService.listOrders();
	}
	
	// Http Get One
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Order get(@PathVariable("id") Long id) {
		logger.debug("Get One Order >>>>>>>>>>>>>>>>>>>>");
		return orderService.getOrder(id);
	}
    
	// Http Create One
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Order> createOrder(@RequestBody Order order) {
		logger.info("Create one Order >>>>>>>>>>>>>>>>>>>>");
		orderService.create(order);
		return new ResponseEntity<Order>(order, JSONHelper.getJSONHeaders(), HttpStatus.CREATED);
	}
    
	// Http Update One Order
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	@ResponseStatus(HttpStatus.OK)
	public void update(@RequestBody Order entity, @PathVariable("id") Long id) {
		entity.setOrderId(id);
		logger.info("Update one Order >>>>>>>>>>>>>>>>>>>>");
		orderService.update(entity);
	}
    
    // Http Delete One
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<Long> deleteOrder(@PathVariable Long id) {
    	logger.debug("Delete One Order >>>>>>>>>>>>>>>>>>>>");
		orderService.deleteById(id);
        return new ResponseEntity<Long>(id, HttpStatus.OK);
    }

}
